package com.polaris.live.ui.party.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.RelativeLayout
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.request
import com.polaris.live.databinding.WidgetActscrollMagicLampMarqueeBinding
import com.polaris.live.net.payerService

/**
 * ActScrollMagicLampMarqueeView
 *
 * @author Created by 天晴 on 2024/11/25 14:41
 * @since 1.0.0
 **/
class ActScrollMagicLampMarqueeView(context: Context, attrs: AttributeSet) : RelativeLayout(context, attrs) {

    val mBinding = WidgetActscrollMagicLampMarqueeBinding.inflate(LayoutInflater.from(context), this)
    private val marqueeFactory: MagicLampMarqueeView
    private var mAnchorId: Long = -1 //主播Id
    private var mGiftIdBack: ((giftId: Long) -> Unit)? = null

    init {
        marqueeFactory = MagicLampMarqueeView(context)
        mBinding.marquee.setMarqueeFactory(marqueeFactory)
    }

    fun getActivitiesScroll(mViewModel: BaseViewModel, userId: Long) {
        mAnchorId = userId
        request(mViewModel)
    }

    private fun request(mViewModel: BaseViewModel) {
        mViewModel.request({ payerService.getLampCarousel(mAnchorId) }, {
            if (it.list.isNotEmpty()) {
                visibility = View.VISIBLE
            }
            marqueeFactory.setData(it.list)

            if (it.list.size > 1 && !mBinding.marquee.isFlipping) {
                mBinding.marquee.startFlipping()
            }
        })
    }

    /**
     * 设置礼物Id回调，观众端使用
     */
    fun setGiftIdBackListener(back: (giftId: Long) -> Unit) {
        mGiftIdBack = back
    }

    fun reset() {
        if (mBinding.marquee.isFlipping) {
            mBinding.marquee.stopFlipping()
        }
        visibility = View.GONE
    }

}